<?php
/**
 *
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2017/9/25
 * Time: 下午5:33
 * deadline:2018年01月23日13:38:01
 */
namespace WAdmin\Controller;

use lyf\Tree;

header('content-type:text/html;charset=utf-8');
class UserController extends BaseController {

	/**
	 * @desc 用户登陆
	 * @param $phone     用户账户:用户名，ID号，手机号
	 * @param $password  用户密码
     * @param $remember 记住密码 1 是，0否
	 * @return string
	 */
	public function login($param)
    {
        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('username,password');

        unset($check);

		$password = md5($param['password']);
		$username = $param['username'];

		//过期时间，记住设置为0不过期，不记住 设置token失效时间为3天
        $expire_time = empty($param['remember'])?259200:0;

		$model = M('User');
		$map['u_no'] = $username;
		$map['phone']           = $username;
		$map['email']           = $username;
		$map['account']         = $username;
		$map['_logic'] ='or';
        $condition['_complex'] = $map;
		$condition['password'] = $password;

		$userinfo = $model->field('id,username,headsmall,u_no,last_ip,user_role,last_time,phone,email,role_id')->where($condition)->find();

		if($userinfo){
			if($userinfo['status']!=1){
				$user_save_data['logins'] = $userinfo['logins'] + 1;
                $user_save_data['last_ip'] = client_ip();
                $user_save_data['last_time'] = time();
                $user_save_data['id'] = $userinfo['id'];

				$reslogin = $model->save($user_save_data);
				if($reslogin){
				    $token_info = [
				        'id'    =>  $userinfo['id'],
                        'username'  =>  $userinfo['username'],
                        'phone'     =>  $userinfo['phone'],
                        'role_id'   =>  $userinfo['role_id']
                    ];
					$token = _set_token($token_info,$expire_time);

                    $userinfo = decode_token($token);
					$userinfo['token']    = $token;

                    $role_id = $userinfo['role_id'];

                    if (!empty($role_id)){

                        $access = M("access")->where(['role_id'=>$role_id])->find();

                        $nav_ids = $access['nav_id'];

                        $nav_id_arr = explode(',',$nav_ids);

                        $navList = M('nav')->where(['id'=>['in',$nav_id_arr],'namespace'=>'WAdmin'])->select();

                        $treeObj = new Tree();

                        $navList_all = $treeObj->list2tree($navList,'id','pid','child','0','******');

                        $userinfo['nav_list'] = $navList_all;

                    }else{
                        return showErrorMsg('没有权限登录',100002);
                    }

					if($token)  return showErrorMsg($msg = L('_OPERATION_SUCCESS_'),$code = 0,$return_type = 2,$return_data = $userinfo);
					if(!$token) return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100004);
				}
				if(!$reslogin) return showErrorMsg($msg = L('_TOKEN_BUILD_ERROR_'),$code = 100002);
			}
			if($userinfo['status']==2) return showErrorMsg($msg = L('_ACCOUNT_DISABLED_'),$code = 100002);
		}
		if(!$userinfo) return showErrorMsg($msg = L('_LOGIN_ERROR_'),$code = 100002);
	}

    /**
     * @desc 修改密码
     * @param $param
     * @return array
     */
    public function updatePwd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('o_pwd,n_pwd');

        unset($check);

        $old_pwd = $param['o_pwd'];
        $new_pwd = $param['n_pwd'];



        $y_pwd = M('User')->where(['id'=>$this->userInfo->id])->getField('password');
        if (md5($old_pwd)!=$y_pwd) return showErrorMsg(L('_PWD_FIRST_ERROR_'),100002);


        $operation = M('User')->where(['id'=>$this->userInfo->id])->setField('password',md5($new_pwd));

        if ($operation) {
            $info = M('User')->field('id,username,headsmall,logins,status')->find($this->userInfo->id);
            $token = _set_token($info,0);
            $userinfo = decode_token($token);
            $userinfo['token']    = $token;
            return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$userinfo);
        }else{
            if ($old_pwd == $new_pwd)return showErrorMsg('新旧密码不能相同',100002);
            return showErrorMsg(L("_OPERATION_FAIL_"));
        }

    }

	/**
	 * @desc 退出登陆 
	 * @return string
	 */
	public function logout(){
        if (empty($this->userInfo)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);
		$map['uid'] = $this->userInfo->id;
		$map['platform'] = 'WAdmin';
		$Re = M('Token')->where($map)->delete();
		if($Re)  return showErrorMsg($msg = L('_OPERATION_SUCCESS_'),$code = 0,$return_type = 2,$return_data = $map);
		if(!$Re) return showErrorMsg($msg = L('_OPERATION_FAIL_'),$code = 100002);
	}


    /**
     * @desc
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
	public function getUserInfo($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M("user");

        $condition['id'] = $this->userInfo->id;

        $userinfo = $model->field('id,username,headsmall,u_no,last_ip,last_time,phone,email,role_id')->where($condition)->find();

        if($userinfo && $userinfo['status']!=1){

            $role_id = $userinfo['role_id'];

            if (!empty($role_id)){

                $access = M("access")->where(['role_id'=>$role_id])->find();

                $nav_ids = $access['nav_id'];

                $nav_id_arr = explode('#',$nav_ids);

                $navList = M('nav')->where(['id'=>['in',$nav_id_arr]])->select();

                $treeObj = new Tree();

                $navList_all = $treeObj->list2tree($navList,'id','pid','child','0','******');

                $userinfo['nav_list'] = $navList_all;

                return showErrorMsg($msg = L('_OPERATION_SUCCESS_'),$code = 0,$return_type = 2,$return_data = $userinfo);

            }else{
                return showErrorMsg('没有权限登录',100002);
            }

        }else{
            return showErrorMsg($msg = L('_ACCOUNT_DISABLED_'),$code = 100002);
        }

    }


    /**
     * @desc 公司列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function companyList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('company');

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'id desc','id,title,fr,tel,address',$listRows);

        return showList($list,$page);
    }

    /**
     * @desc 添加公司
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function companyAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('title,fr,tel,address');

        unset($check);

        $model = M('company');

        if (!empty($param['title'])) $insert_data['title'] = $param['title'];
        if (!empty($param['fr'])) $insert_data['fr'] = $param['fr'];
        if (!empty($param['tel'])) $insert_data['tel'] = $param['tel'];
        if (!empty($param['address'])) $insert_data['address'] = $param['address'];

        $insert_data['create_time'] = time();
        $insert_data['uid'] = $this->userInfo->id;

        $operation = $model->add($insert_data);
        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 更新公司
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function companyUpdate($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,title,fr,tel,address');

        unset($check);

        $model = M('company');

        if (!empty($param['title'])) $save_data['title'] = $param['title'];
        if (!empty($param['fr'])) $save_data['fr'] = $param['fr'];
        if (!empty($param['tel'])) $save_data['tel'] = $param['tel'];
        if (!empty($param['address'])) $save_data['address'] = $param['address'];

        $save_data['id'] = $param['id'];

        $operation = $model->save($save_data);
        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 删除公司
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function companyDel($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('company');

        $operation = $model->where(['id'=>['in',$param['id']]])->delete();
        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 角色列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function roleList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('role');

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $keyword = $param['keyword'];

        $map['uid'] = $this->userInfo->id;

        if (!empty($keyword)) $map['role_name'] = array('like','%'.$keyword.'%');

        list($list,$page) = _listPage($model,$map,$p,'id asc','id,role_name',$listRows);

        return showList($list,$page);
    }

    /**
     * @desc 添加角色
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function roleAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('role_name');

        unset($check);

        $model = M('role');

        $insert_data['role_name'] = $param['role_name'];

        $insert_data['status'] = 1;
        $insert_data['uid'] = $this->userInfo->id;

        $operation = $model->add($insert_data);
        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,['role_id'=>$operation]);
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    public function roleFuncList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('role_id,type');

        unset($check);


        $model = M('system_group');

        $type = $param['type'];

        if ($type==1){
            $list = $model->where(['pid'=>0,'type'=>1])->select();

            if (!empty($list)){
                foreach ($list as $k=>$v){

                    $next = $model->where(['pid'=>$v['id']])->select();

                    if (!empty($next)){
                        foreach ($next as $k1=>$v1){

                            $third = $model->where(['pid'=>$v['id']])->select();

                            $next[$k1]['_child'] = empty($third)?[]:$third;
                        }

                        $list[$k]['_child'] = $next;
                    }else{
                        $list[$k]['_child'] = [];
                    }
                }
            }

        }elseif ($type==2){

            $map['uid'] = $this->userInfo->id;

            list($list,$page) = _listPage($model,$map,1,'gid desc','group_concat(id) ids,gid','1000','gid');

            if (!empty($list)){
                foreach ($list as $k=>$v){
                    if (empty($v['gid'])){
                        foreach (explode(',',$v['ids']) as $key=>$val){
                            $list[$k+$key] = $model->field('id,gid,headsmall,car_no')->where(['id'=>$val])->find();
                            $list[$k+$key]['_child'] = [];
                        }
                    }else{
                        $list[$k]['id'] = '';

                        $list[$k]['gid'] = $v['gid'];//群的id


                        $list[$k]['headsmall'] = '';//群的图标
                        $list[$k]['car_no'] = M('car_group')->field('title')->where(['id'=>$v['gid']])->getField('title');

                        $list[$k]['_child'] = [];
                        foreach (explode(',',$v['ids']) as $key=>$val){
                            $list[$k]['_child'][$key] = $model->field('id,headsmall,car_no')->where(['id'=>$val])->find();
                            $list[$k]['_child'][$key]['_child'] = [];
                        }
                        unset($list[$k]['ids']);
                    }
                }
            }

            $all = $model->where($map)->field('id,gid,car_no,headsmall')->select();

            foreach ($all as $all_k => $all_v){
                $all[$all_k]['_child'] = [];
            }
            $first = [
                'id' => '',
                'gid'=> '',
                'car_no' => '全部设备',
                'headsmall'=>'',
                '_child'    =>  $all
            ];
            array_unshift($list,$first);



        }else{



        }

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$list);

    }



    public function roleListByPID($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $list = M('system_group')->where(['pid'=>$param['id']])->select();

        if (!empty($list)){
            foreach ($list as $k=>$v){

                $next = M('system_group')->where(['pid'=>$v['id']])->select();

                $list[$k]['_child'] = empty($next)?[]:$next;

            }
        }

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$list);

    }


    /**
     * @desc 更新角色名称
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function roleUpdate($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,role_name');

        unset($check);

        $model = M('role');

        $save_data['id'] = $param['id'];
        $save_data['role_name'] = $param['role_name'];
        $save_data['update_time'] = time();

        $operation = $model->save($save_data);
        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 删除角色
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function roleDel($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('role');

        $operation = $model->where(['id'=>['in',$param['id']]])->delete();
        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 用户列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function userList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $cid = M('user')->where(['id'=>$this->userInfo->id])->getField('company_id');

        $model = M('user');

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $keyword = $param['keyword'];

        $map['company_id'] = empty($cid)? array('neq',0): $cid;

        $map['id'] = array('neq',$this->userInfo->id);

        if (!empty($keyword)) {
            $condition['username'] = array('like','%'.$keyword.'%');
            $condition['phone'] = array('like','%'.$keyword.'%');
            $condition['email'] = array('like','%'.$keyword.'%');
            $condition['account'] = array('like','%'.$keyword.'%');

            $condition['_logic'] ='or';
            $map['_complex'] = $condition;
        }

        list($list,$page) = _listPage($model,$map,$p,'id asc','id,company_id,username,career_id,role_id',$listRows);

        if (!empty($list)){
            foreach ($list as $k=>$v){
                $companyName = M('company')->where(['id'=>$v['company_id']])->getField('title');
                $list[$k]['company_title'] = $companyName;

                $careerName = M('career')->where(['id'=>$v['career_id']])->getField('title');
                $list[$k]['career_title'] = $careerName;

                $roleName = M('role')->where(['id'=>$v['role_id']])->getField('role_name');
                $list[$k]['role_name'] = $roleName;
            }
        }
        return showList($list,$page);
    }


    /**
     * @desc 添加用户
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function userAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('username,career_id,company_id,role_id,email,password,phone');

        unset($check);

        $username = $param['username'];

        $is_exist = M('user')->where(['username'=>$username])->find();

        if ($is_exist) return showErrorMsg('账号已存在',100002);
        $insert_data = $param;
        $insert_data['password'] = md5($param['password']);
        $insert_data['account'] = $param['username'];

        $model = M('user');

        $operation = $model->add($insert_data);
        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),100002);
    }


    /**
     * @desc 修改用户
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function userUpdate($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,username,career_id,company_id,role_id,email,password,phone,password');

        unset($check);


        $save_data = $param;

        if (empty($save_data['password'])){
            unset($save_data['password']);
        }else{
            $save_data['password'] = md5($save_data['password']);
        }

        $model = M('user');

        $operation = $model->save($save_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),100002);
    }

    /**
     * @desc 删除用户
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function userDel($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('ids');

        unset($check);

//        $idArr = explode(',',$param['ids']);


        $model = M('user');

        $operation = $model->where(['id'=>['in',$param['ids']]])->delete();

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),100002);
    }

    /**
     * @desc 获取我的公司列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function getCompanyList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('company');

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 2000 : $param['listRows'];

        $cid = M('user')->where(['id'=>$this->userInfo->id])->getField('company_id');
        if (empty($cid)){
            list($list,$page) = _listPage($model,null,$p,'id desc','id,title',$listRows);
        }else{
            $page = [
                'total' =>  1,
                'count' =>  1,
            'listRows'  =>  20,
                'p'     =>  1
            ];
            $companyInfo = M('company')->field('id,title')->find($cid);
            $list[] = $companyInfo;
        }

        return showList($list,$page);
    }

    /**
     * @desc 职业列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function getCareerList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('career');

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];


        list($list,$page) = _listPage($model,null,$p,'id desc','id,title',$listRows);

        return showList($list,$page);
    }


    /**
     * @desc 费用中心
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function feeList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('tips');

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('fee');

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'id desc',null,$listRows);

        if (!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['is_tip'] = round(($v['create_time'] - time())/3600/24) < $param['tips'] ? 1:0;
                $list[$k]['sy_days'] = intval(($v['deadline']-time())/(3600*24));
            }
        }

//        $u_deadline = M('User')->where(['id'=>$this->userInfo->id])->getField('deadline');
//
//        $sy_days = $u_deadline<time() ? 0 :intval(($u_deadline-time())/86400 );


        $ret_data = [
            'list'  =>  $list,
            'page'  =>  $page,
//        'sy_days'   =>$sy_days
        ];
        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$ret_data);
//        return showList($list,$page);
    }

    /**
     * @desc 充值，绝壁有坑
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function feeAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('days');

        unset($check);

        $uid = empty($param['uid'])?$this->userInfo->id:$param['uid'];

        $time_days = $param['days']*86400;

        $insert_data = [
            'create_time'   =>  time(),
            'uid'           =>  $uid,
            'fee_no'        =>  date('YmdHis',time()).rand(100000,999999),
            'in_content'    =>  empty($param['in_content'])?'':$param['in_content'],
            'money'         =>  empty($param['fee'])?'':$param['fee'],
            'deadline'      =>  time()+$time_days,
            'notes'         =>  empty($param['notes'])?'':$param['notes']
        ];

        $model = M('fee');

        $operation = $model->add($insert_data);

        if ($operation) {

            M('user')->where(['id'=>$uid])->setInc('deadline',$time_days);

            return showErrorMsg(L("_OPERATION_SUCCESS_"));
        }

        return showErrorMsg(L("_OPERATION_FAIL_"),100002);
    }


    /**
     * @desc access
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function accessList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('role_id');

        unset($check);

        $user_role = M('user')->where(['id'=>$this->userInfo->id])->getField('user_role');

        $navModel = M('nav');

        $accessModel = M('access');

        if ($user_role!=1) return showErrorMsg('你不是系统管理,无法操作',100002);

        $nav_list = $navModel->field('id,nav_name')->where(['pid'=>0,'namespace'=>"WAdmin"])->select();

        $nav_id = $accessModel->where(['role_id'=>$param['role_id']])->getField('nav_id');

        $nav_arr = explode(',',$nav_id);

        foreach ($nav_list as $k=>$v){

            if (in_array($v['id'],$nav_arr)){
                $nav_list[$k]['is_selected'] = '1';
            }else{
                $nav_list[$k]['is_selected'] = '2';
            }

            $next = $navModel->field('id,nav_name')->where(['pid'=>$v['id']])->select();

            if (!empty($next)){
                foreach ($next as $k1=>$v1){
                    if (in_array($v1['id'],$nav_arr)){
                        $next[$k1]['is_selected'] = '1';
                    }else{
                        $next[$k1]['is_selected'] = '2';
                    }
                }
            }
            $nav_list[$k]['_child'] = empty($next)?[]:$next;


        }

        return showList($nav_list,new \stdClass());
    }


    /**
     * @desc
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function accessSave($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('role_id,ids');

        unset($check);


        $is_exist = M('access')->where(['role_id'=>$param['role_id']])->find();

        if ($is_exist){
            $save['id'] = $is_exist['id'];
            $save['nav_id'] = $param['ids'];

            M('access')->save($save);
        }else{
            $insert_data['role_id'] = $param['role_id'];
            $insert_data['nav_id'] = $param['ids'];

            M('access')->add($insert_data);
        }

        return showErrorMsg(L("_OPERATION_SUCCESS_"));
    }


    public function access($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);


//        $id = $this->userInfo->role_id;

        $id = 21;

//        $id = $param['id'];
        $accessModel = D('access');
        $navModel = D('nav');

        $accessinfo = $accessModel->where('role_id = ' . $id . '')->field('nav_id')->find();
        $ids = '#' . $accessinfo['nav_id'] . '#';
        $role_type = D('role')->where('id = %d', $id)->getField('type');
        if ($role_type == 1) {
            $navlist = $navModel->where('type = %d', 1)->select();
        } else {
            $navlist = $navModel->select();
        }
        foreach ($navlist as $key => $value) {
            if ($value['controller'] == 'index' && $value['function'] == 'index') {
                unset($navlist[$key]);
            }
        }

        var_dump($navlist);
//        $this->assign(array(
//            'navlist' => $navlist,
//            'group' => $ids,
//            'role_id' => $id
//        ));
//        $this->display();
    }


    public function access2($param)
    {
        $id = I('id');
        $accessModel = D('access');
        $navModel = D('nav');
        if (IS_POST) {
            if (!IS_AJAX) {
                $this->error('提交方式不正确！');
            } else {
                $getrule = I('rule');
                $accessModel->where(array('role_id' => $id))->delete();
                if (empty($getrule)) {
                    $this->error('请选择权限！');
                }
                $data['role_id'] = $id;
                $data['nav_id'] = '1#51#73#52#' . implode("#", $getrule);

                if ($data) {
                    $res = $accessModel->add($data);
                    if ($res) {
                        $this->success('修改成功！', U('Admin/Admin/rolelist'), 1);
                    } else {
                        $this->error('修改失败');
                    }
                }

            }
        } else {
            $accessinfo = $accessModel->where('role_id = ' . $id . '')->field('nav_id')->find();
            $ids = '#' . $accessinfo['nav_id'] . '#';
            $role_type = D('role')->where('id = %d', $id)->getField('type');
            if ($role_type == 1) {
                $navlist = $navModel->where('type = %d', 1)->select();
            } else {
                $navlist = $navModel->select();
            }
            foreach ($navlist as $key => $value) {
                if ($value['controller'] == 'index' && $value['function'] == 'index') {
                    unset($navlist[$key]);
                }
            }
            $this->assign(array(
                'navlist' => $navlist,
                'group' => $ids,
                'role_id' => $id
            ));
            $this->display();
        }
    }


}